package com.future.mayi.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.IndexGoodsAdapter;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.ActivityDetailResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

/**
 * 活动Activity
 */
public class ActivitiesActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener, OnItemClickListener {


    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;

    private TextView tvTitle, tvSubTitle;
    private ImageView ivImg;


    private IndexGoodsAdapter adapter;

    private int activityId = -1;
    private int page;


    public static void startActivity(Context context, int activity_id) {
        Intent intent = new Intent(context, ActivitiesActivity.class);
        intent.putExtra("activity_id", activity_id);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("活动详情");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_activities);
    }

    @Override
    protected void initView() {

        View view = View.inflate(this, R.layout.layout_activities_head, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        recyclerView.addHeaderView(view);

        ViewGroup.LayoutParams layoutParams = view.findViewById(R.id.rl_content).getLayoutParams();
        layoutParams.width = CommonUtils.getScreenSize(mContext)[0];
        layoutParams.height = (int) ((float) layoutParams.width * (185.0f / 375.0f));
        tvTitle = view.findViewById(R.id.tv_title);
        tvSubTitle = view.findViewById(R.id.tv_sub_title);
        ivImg = view.findViewById(R.id.iv_img);


        adapter = new IndexGoodsAdapter(this, recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void initDates() {
        if (getIntent() != null) {
            activityId = getIntent().getIntExtra("activity_id", activityId);
            if (activityId == -1) {
                try {
                    activityId = Integer.valueOf(getIntent().getStringExtra("activity_id"));
                } catch (Exception e) {
                }
            }
        }

        recyclerView.refresh();
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onRefresh() {
        page = 0;
        search();
    }

    @Override
    public void onLoadMore() {
        page++;
        search();
    }

    @Override
    public void OnItem(View view, int position) {
        Intent intent = new Intent(this, GoodsActivity.class);
        intent.putExtra("goodsId", adapter.getList().get(position).getGoods_id());
        startActivity(intent);
    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    private void search() {
        new HttpRequest<ActivityDetailResponse>()
                .with(this)
                .setApiCode(ApiCst.activityDetail)
                .addParam("activity_id", activityId)
                .setListener(new HttpRequest.OnNetworkListener<ActivityDetailResponse>() {
                    @Override
                    public void onSuccess(ActivityDetailResponse response) {
                        if (response.detailModel != null) {
                            getTitleText().setText(response.detailModel.getActivity().getActivity_name());
                            tvTitle.setText(response.detailModel.getActivity().getActivity_name());
                            tvSubTitle.setText(response.detailModel.getActivity().getSubtitle());
                            HCUtils.loadWebImg(mContext, ivImg, response.detailModel.getActivity().getActivity_img());
                            HCUtils.refreshListForPage(recyclerView, adapter, response.detailModel.getGoods(), page, 10);
                        } else
                            HCUtils.refreshFail(recyclerView, page, getApplicationContext(), response.message);
                    }

                    @Override
                    public void onFail(String message, ActivityDetailResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getApplicationContext(), message);
                    }
                }).start(new ActivityDetailResponse());
    }


}
